<?php
/**
 * Description: 每日一题快讯
 * User: devforma
 * Date: 15/6/6
 * Time: 23:24
 */

class PromoteLiveCourse implements iController {
    private $unpurchasedUrl, $purchasedUrl;

    public function __construct() {
        $this->unpurchasedUrl = 'zhiboke@'. YG_MOBILE_SECURE_URL .'/index.html#/live/unpurchased?course_id=';
        $this->purchasedUrl = 'zhiboke@'. YG_MOBILE_SECURE_URL .'/index.html#/live/purchased?course_id=';
    }

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {

        $setting = getOption('promote_live_course');

        if (empty($setting))
            return genOutput(1000);

        $targetContent = $setting['targetDef'];
        if ($setting['jumpTarget'] == 'app' && strpos($setting['targetDef'], 'zhiboke@') !== false) { //直播课路径
            $courseId = substr($setting['targetDef'], 8);
            if (!empty($queryArgs['user_id']) && $this->checkPurchase($queryArgs['user_id'], $courseId)) {
                $targetContent = $this->purchasedUrl . substr($setting['targetDef'], 8);
            } else {
                $targetContent = $this->unpurchasedUrl . substr($setting['targetDef'], 8);
            }

        } elseif ($setting['jumpTarget'] == 'app' && strpos($setting['targetDef'], 'market@') !== false) { //市场关键字
            $targetContent = 'market@' . $this->getMarket(substr($setting['targetDef'], 7),
                    $queryArgs['terminal_type'] === 'android_phone');
        }

        $data = array(
            'display_type' => $setting['displayType'],
            'display_content' => $setting['desc'],
            'target_type' => $setting['jumpTarget'],
            'target_content' => $targetContent
        );

        if (!empty($setting['apkUrl']) && $queryArgs['terminal_type'] === 'android_phone') {
            $data['target_type'] = 'apk';
            $data['target_content'] = $setting['apkUrl'];
        }

        return empty($data['display_content']) ? genOutput(1000) : genOutput(1, $data);
    }

    private function checkPurchase($userId, $courseId) {
        global $cmsdb;

        $purchased = $cmsdb->getVar('SELECT `id` FROM `relation_user_product` WHERE `user_id`=:uid AND `product_id`=:pid AND `product_type`="zhiboke"',
            array(':uid' => $userId, ':pid' => $courseId));

        return !empty($purchased);
    }

    /**
     * 获取市场跳转关键字
     * @param string $appName app名称
     * @param bool $isAndroid 是否是android
     * @return string
     */
    private function getMarket($appName, $isAndroid) {
        if ($isAndroid) {
            switch ($appName) {
                case 'dailyplan':
                    return 'com.appublisher.dailyplan';

                case 'quizbank':
                    return 'com.appublisher.quizbank';

                case 'dailylearn':
                    return 'com.appublisher.dailylearn';

                default:
                    return 'com.appublisher.quizbank';
            }

        } else {
            switch ($appName) {
                case 'dailyplan':
                    return '956709844';

                case 'quizbank':
                    return '849590697';

                case 'dailylearn':
                    return '680032672';

                default:
                    return '849590697';
            }
        }
    }


}

